
#include <M5Stack.h>
#include "hx711.h"

#define FRONT 8          //字符大小
#define X_LOCAL 40       
#define Y_LOCAL 50       //示值的位置

//***************
//显示三个按钮及文字
//***************
void drawbutton()
{
   
  M5.Lcd.setTextColor(TFT_WHITE,TFT_GREEN);
  M5.Lcd.fillRoundRect(30, 190, 70, 40, 4,TFT_GREEN);    //按钮A
  M5.Lcd.drawString("Clear",35,200, 4);

  M5.Lcd.setTextColor(TFT_WHITE,TFT_BLUE);
  M5.Lcd.fillRoundRect(130, 190, 70, 40,4, TFT_BLUE);    //按钮B
  M5.Lcd.drawString("ON", 145,200,4);

  M5.Lcd.setTextColor(TFT_WHITE,TFT_RED);
  M5.Lcd.fillRoundRect(230, 190, 70, 40,4, TFT_RED);     //按钮C
  M5.Lcd.drawString("OFF",243,200,4); 
}

//*************
//手动去皮
//*************
void qupi()         
{
    M5.Lcd.clear();
    M5.Lcd.setTextColor(TFT_GREEN,TFT_BLACK);
    M5.Lcd.drawString("clearing...",10,10,4);
    delay(500);
    Get_Maopi();
    M5.Lcd.clear();
    drawbutton();
  }

//***************
//按键校准
//***************
void jiaozhun()         
{
  
 }

//***************
//主程序
//***************
void setup()
{
  M5.begin();           
  M5.Power.begin();       //m5stack初始化
  drawbutton();
  M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);  //LCD初始化
  
  Init_Hx711();       //初始化HX711模块连接的IO设置
  delay(250);
  Get_Maopi();       //获取毛皮
 
}
float Weight = 0;
float a,w,b;
int c;
void loop()
{
   M5.update();
    if(M5.BtnA.wasPressed())
   {
          qupi(); //去皮
   }
   else if(M5.BtnB.wasPressed())
   {
    M5.Lcd.wakeup();
    M5.Lcd.clear();
    M5.Lcd.setTextColor(TFT_GREEN,TFT_BLACK);
    M5.Lcd.drawString("power ON",10,10,4);
    delay(300);
    M5.Lcd.clear();
    drawbutton();
    }
    else if(M5.BtnC.wasPressed())
   {
    M5.Lcd.clear();
    M5.Lcd.setTextColor(TFT_RED,TFT_BLACK);
    M5.Lcd.drawString("power OFF",10,10,4);
    delay(300);
    M5.Lcd.sleep();    
    }
   else
   { 
            
           Weight = Get_Weight(); //计算放在传感器上的重物重量
           w=Weight/1000;
          
            M5.Lcd.setTextColor(TFT_WHITE,TFT_BLACK);
            M5.Lcd.setCursor(X_LOCAL,Y_LOCAL,FRONT);  
            if(w<0)
            {M5.Lcd.printf("0.000"); }
            else if (w>5)
            { M5.Speaker.beep();
              M5.Lcd.printf("%0.3f",w); 
              M5.Lcd.drawString("Kg",250,150,4);
              }
            else
            {M5.Lcd.printf("%0.3f",w); 
            M5.Lcd.drawString("Kg",250,150,4);}
            } 

        
    }
        //延时1s
     
   
